package com.agent.platform.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 */
@Configuration
public class RabbitMQConfig {
    
    // 任务队列
    public static final String TASK_QUEUE = "agent.task.queue";
    // 任务交换机
    public static final String TASK_EXCHANGE = "agent.task.exchange";
    // 任务路由键
    public static final String TASK_ROUTING_KEY = "agent.task.routing";
    
    // 死信队列
    public static final String TASK_DLX_QUEUE = "agent.task.dlx.queue";
    // 死信交换机
    public static final String TASK_DLX_EXCHANGE = "agent.task.dlx.exchange";
    // 死信路由键
    public static final String TASK_DLX_ROUTING_KEY = "agent.task.dlx.routing";
    
    /**
     * 任务队列
     */
    @Bean
    public Queue taskQueue() {
        return QueueBuilder.durable(TASK_QUEUE)
                .withArgument("x-dead-letter-exchange", TASK_DLX_EXCHANGE)
                .withArgument("x-dead-letter-routing-key", TASK_DLX_ROUTING_KEY)
                .withArgument("x-message-ttl", 3600000) // 1小时过期
                .build();
    }
    
    /**
     * 任务交换机
     */
    @Bean
    public DirectExchange taskExchange() {
        return new DirectExchange(TASK_EXCHANGE, true, false);
    }
    
    /**
     * 绑定任务队列到交换机
     */
    @Bean
    public Binding taskBinding() {
        return BindingBuilder.bind(taskQueue())
                .to(taskExchange())
                .with(TASK_ROUTING_KEY);
    }
    
    /**
     * 死信队列
     */
    @Bean
    public Queue taskDlxQueue() {
        return QueueBuilder.durable(TASK_DLX_QUEUE).build();
    }
    
    /**
     * 死信交换机
     */
    @Bean
    public DirectExchange taskDlxExchange() {
        return new DirectExchange(TASK_DLX_EXCHANGE, true, false);
    }
    
    /**
     * 绑定死信队列到死信交换机
     */
    @Bean
    public Binding taskDlxBinding() {
        return BindingBuilder.bind(taskDlxQueue())
                .to(taskDlxExchange())
                .with(TASK_DLX_ROUTING_KEY);
    }
    
    /**
     * JSON消息转换器
     */
    @Bean
    public Jackson2JsonMessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    
    /**
     * RabbitTemplate配置
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(messageConverter());
        // 消息发送确认
        template.setMandatory(true);
        return template;
    }
    
    /**
     * 消息监听器容器工厂
     */
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter());
        factory.setConcurrentConsumers(3);
        factory.setMaxConcurrentConsumers(10);
        factory.setPrefetchCount(5);
        return factory;
    }
}
